
package com.afd.ozblip.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.adapter.BusinessList3Adapter;
import com.afd.ozblip.entity.BusinessEntity;
import com.afd.ozblip.entity.CouponEntity;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class UserCouponDetailActivity extends BaseActivity {

    /****************** 视图变量 **************************/
    private TextView topBusinessTitleTv;
    private TextView businessTitleTv;
    private TextView businessContentTv;
    private TextView lifeDateTv;
    private LinearLayout commonRight;
    private ListView businessListView;
    private TextView lvTipTv;
    /****************** 数据变量 **************************/
    private CouponEntity couponInfo;
    private Map<String, Object> resultMap;
    private List<BusinessEntity> businessList;
    private BusinessList3Adapter businessListAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_coupon_detail);
        // 启动返回功能
        toBack();
        // 寻找视图
        findViews();
        // 设置监听
        setListeners();
        // 获取参数
        getBundle();
        // 设置视图
        setViews();
        // 获取适用商户列表
        requestOtherBusinessData();
    }

    /**
     * 寻找视图
     */
    public void findViews() {
        this.topBusinessTitleTv = (TextView) findViewById(R.id.tv_coupon_detail_top_businessTitle);
        this.businessTitleTv = (TextView) findViewById(R.id.tv_coupon_detail_businessTitle);
        this.businessContentTv = (TextView) findViewById(R.id.tv_coupon_detail_content);
        this.lifeDateTv = (TextView) findViewById(R.id.tv_coupon_detail_lifeDate);
        this.commonRight = (LinearLayout) findViewById(R.id.ll_common_right);
        this.businessListView = (ListView) findViewById(R.id.lv_business_list);
        this.lvTipTv = (TextView) findViewById(R.id.tv_list_data_tip);
    }

    /**
     * 设置监听
     */
    public void setListeners() {
        View.OnClickListener couponDetailClick = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.ll_common_right:
                        Dialog alertDialog = new AlertDialog.Builder(
                                UserCouponDetailActivity.this)
                                .setTitle("提示")
                                .setMessage("确定删除当前优惠券吗？")
                                .setIcon(android.R.drawable.ic_menu_delete)
                                .setPositiveButton("确定",
                                        new DialogInterface.OnClickListener() {

                                            @Override
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {
                                                requestDeleteCouponData();
                                            }
                                        })
                                .setNegativeButton("取消",
                                        new DialogInterface.OnClickListener() {

                                            @Override
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {
                                                dialog.dismiss();
                                            }
                                        }).create();
                        alertDialog.show();
                        break;

                    default:
                        break;
                }
            }
        };
        this.commonRight.setOnClickListener(couponDetailClick);
    }

    /**
     * 获取传递的参数
     */
    private void getBundle() {
        Intent intent = getIntent();
        couponInfo = (CouponEntity) intent.getSerializableExtra("coupon");
    }

    private void setViews() {
        if (couponInfo != null) {
            this.topBusinessTitleTv.setText(couponInfo.getBusinessName());
            this.businessTitleTv.setText(couponInfo.getBusinessName());
            this.businessContentTv.setText(couponInfo.getCouponContent());
            this.lifeDateTv.setText(couponInfo.getLiftDate());
        }
    }

    /**
     * 获取删除优惠券数据
     */
    private void requestDeleteCouponData() {
        if (commonUserInfo == null) {
            return;
        }
        if (couponInfo == null) {
            return;
        }
        AsyncLoadData ald = new AsyncLoadData(this, "提交中...",
                new AsyncAction() {

                    @Override
                    public void doLoad() throws Exception {
                        // 获取提交参数
                        Map<String, Object> addReviewParamMap = new HashMap<String, Object>();
                        addReviewParamMap.put("userId",
                                commonUserInfo.getUserId());
                        addReviewParamMap.put("couponId",
                                couponInfo.getCouponId());

                        // 发送请求
                        byte[] bUserFriend = HttpUtil.requestByPost(
                                UserCouponDetailActivity.this,
                                Constants.USER_DELETE_COUPON_API,
                                addReviewParamMap);
                        if (bUserFriend != null) {
                            resultMap = new ParseJson()
                                    .parseAddResultJson(bUserFriend);
                        }
                    }

                    @Override
                    public void loadEnd() throws Exception {
                        boolean returnFlg = (Boolean) resultMap
                                .get("returnFlg");
                        String returnMsg = resultMap.get("regMessage")
                                .toString();
                        if (returnFlg) {
                            showShortToast(UserCouponDetailActivity.this,
                                    returnMsg, true);
                            finish();
                        } else {
                            showShortToast("删除优惠券失败");
                        }
                    }

                });
        ald.execute(0);

    }

    /**
     * 获取适用商户数据
     */
    private void requestOtherBusinessData() {
        this.lvTipTv.setVisibility(0);
        this.lvTipTv.setText(getResources().getText(R.string.loding_text));
        AsyncLoadData ald = new AsyncLoadData(this, "", new AsyncAction() {

            @Override
            public void doLoad() throws Exception {
                // 获取提交参数
                Map<String, Object> addReviewParamMap = new HashMap<String, Object>();
                addReviewParamMap.put("couponId", couponInfo.getCouponId());
                addReviewParamMap.put("language", commonLanguage);
                // 发送请求
                byte[] bUserFriend = HttpUtil.requestByPost(
                        UserCouponDetailActivity.this,
                        Constants.SELLER_COUPON_DETAIL_API, addReviewParamMap);
                if (bUserFriend != null) {
                    businessList = new ParseJson()
                            .parseCouponBusinessListJson(bUserFriend);
                }
            }

            @Override
            public void loadEnd() throws Exception {
                setBusinessListAdapter();
            }

        });
        ald.execute(0);
    }

    /**
     * 调用评论列表适配器
     */
    public void setBusinessListAdapter() {
        if (CollectionUtil.UnNullAndZero(businessList)) {
            this.lvTipTv.setVisibility(8);
            this.businessListAdapter = new BusinessList3Adapter(
                    UserCouponDetailActivity.this, commonLocation.getLongitude(),
                    commonLocation.getLatitude());
            this.businessListAdapter.addItems(businessList);
            this.businessListView.setAdapter(this.businessListAdapter);
        } else {
            this.lvTipTv.setVisibility(0);
            this.lvTipTv.setText(getResources().getText(R.string.noData_text));
        }
    }
}
